package com.dangdang.sharding.listener.source;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dangdang.sharding.listener.event.Event;
import com.dangdang.sharding.listener.event.EventListener;

@Component
@Scope("prototype")
public class ApplicationEventPublisher {

	private List<EventListener<Event>> eventListeners = new ArrayList<EventListener<Event>>();

	public void addListener(EventListener<Event> e) {
		eventListeners.add(e);
	}

	public void removeListener(EventListener<Event> e) {
		eventListeners.remove(e);
	}

	public void publishEvent(Event event){
		for (EventListener<Event> eventListener : eventListeners) {
			eventListener.onListener(event);
		}
	}
}
